/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package 解析.node.node;

import 解析.node.others.TreeInfo;
import 解析.Matcher;

/**
 *
 * @author yanxi
 */
public class BackRef extends Node { 
    public int groupIndex;

    public BackRef(int groupCount) {
        super();
        groupIndex = groupCount + groupCount;  
    }
    
    @Override
    public boolean match(Matcher matcher, int i, CharSequence seq) {
        int j = matcher.groups[groupIndex];
        int k = matcher.groups[groupIndex + 1];

        int groupSize = k - j;
        // If the referenced group didn't match, neither can this
        if (j < 0) {
            return false;
        }

        // If there isn't enough input left no match
        if (i + groupSize > matcher.to) {
            matcher.hitEnd = true;
            return false;
        }
        // Check each new char to make sure it matches what the group
        // referenced matched last time around
        for (int index = 0; index < groupSize; index++) {
            if (seq.charAt(i + index) != seq.charAt(j + index)) {
                return false;
            }
        }

        return next.match(matcher, i + groupSize, seq);
    }

    @Override
    public boolean study(TreeInfo info) {
        info.maxValid = false;
        return next.study(info);
    } 
    
    @Override
    public String toString(){
        //return "["+this.getClass().getName()+"]"+"groupIndex:"+groupIndex;
        return "\\"+groupIndex/2;
    }
}
